<?php

/**
 * character actions.
 *
 * @package    wte
 * @subpackage character
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class characterActions extends sfActions {
	/**
	 * Executes index action
	 *
	 * @param sfRequest $request A request object
	 */
	public function executeIndex(sfWebRequest $request) {
		$form = new characterTemplateForm();
		$characters = Doctrine::getTable('CharacterTemplate') -> findAll();

		$this -> form = $form;
		$this -> characters = $characters;
	}

	public function executeCreate(sfWebrequest $request) {
		$form = new characterTemplateForm();
		$form -> bind($request -> getParameter('character_template'));
		$form -> save();
		if ($request -> isXmlHttpRequest()) {
			$characters = Doctrine::getTable('CharacterTemplate') -> findAll();
			return $this -> renderText($this -> getPartial('charactersList', array('list' => $characters)));
		}
		$this -> forward('character', 'index');
	}

	public function executeEdit(sfWebrequest $request) {
		$character = $this -> getRoute() -> getObject();
		$form = new characterTemplateForm($character);
		$this -> character = $character;
		if ($request -> isMethod('post')) {
			$form -> bind($request -> getParameter('character_template'));
			$form -> save();
			
			if ($request -> isXmlHttpRequest()) {
				$characters = Doctrine::getTable('CharacterTemplate') -> findAll();
				return $this -> renderText($this -> getPartial('charactersList', array('list' => $characters)));
			}

		} elseif ($request -> isXmlHttpRequest()) {//on suppose que l'on est en get et ajax
			return $this -> renderText($this -> getPartial('edithfrey', array('form' => $form, 'character' => $character)));
		}
		$this -> forward('character', 'index');
	}

	public function executeDelete(sfWebrequest $request) {
		$character = $this -> getRoute() -> getObject();
		$character -> delete();
		if ($request -> isXmlHttpRequest()) {
			$characters = Doctrine::getTable('CharacterTemplate') -> findAll();
			return $this -> renderText($this -> getPartial('charactersList', array('list' => $characters)));
		}

		$this -> forward('character', 'index');

	}

}
